package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceInpl implements OrderService {

    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;

    @Override
    public Result addOrder(Map map) throws Exception {
        //先判断当天是否有预约设置
        String orderDate = (String) map.get("orderDate");
        String setmealId = (String) map.get("setmealId");
        OrderSetting orderSetting = orderSettingDao.findOrderSettingByDate(DateUtils.parseString2Date(orderDate));
        if (orderSetting == null) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //判断是否是会员不是就自动添加会员
        String telephone = (String) map.get("telephone");
        Member member = memberDao.findMemberByTelephone(telephone);
        if (member != null) {
            //判断有无重复预约
            Order order = new Order();
            order.setMemberId(member.getId());
            order.setOrderDate(DateUtils.parseString2Date(orderDate));
            order.setSetmealId(Integer.parseInt(setmealId));
            List<Order> orderList = orderDao.findOrderByCondition(order);
            System.out.println(orderList);
            if (orderList != null && orderList.size() > 0) {
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
        } else {
            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            memberDao.add(member);//自动完成会员注册
        }
        //判断是否已约满
        Result result;
        synchronized (this) {
            result = getResult(map, orderDate, orderSetting, member);
        }
        return result;
    }

    public Result getResult(Map map, String orderDate, OrderSetting orderSetting, Member member) throws Exception {
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if (reservations == number || number == 0) {
            return new Result(false, MessageConstant.ORDER_FULL);
        } else {
            //预约成功更新预约人数
            Order order = new Order();
            order.setMemberId(member.getId());//设置会员ID
            order.setOrderDate(DateUtils.parseString2Date(orderDate));//预约日期
            order.setOrderType((String) map.get("orderType"));//预约类型
            order.setOrderStatus(Order.ORDERSTATUS_NO);//到诊状态
            order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));//套餐ID
            orderDao.add(order);
            orderSetting.setReservations(orderSetting.getReservations() + 1);//设置已预约人数+1
            orderSettingDao.editReservationsByOrderDate(orderSetting);
            return new Result(true, MessageConstant.ORDER_SUCCESS, order.getId());
        }
    }

    //根据预约ID查询预约相关信息（体检人姓名、预约日期、套餐名称、预约类型）
    public Map findById(Integer id) throws Exception {
        Map map = orderDao.findById4Detail(id);
        if (map != null) {
            //处理日期格式
            Date orderDate = (Date) map.get("orderDate");
            map.put("orderDate", DateUtils.parseDate2String(orderDate));
        }
        return map;
    }

}
